package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.product.domain.Category;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-04-29
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    /**
     * 查询分类列表
     *
     * @param parentId
     * @return List<Category> 前端判断该分类是否有子分类：在分类实体类中新增属性"hasChildren"
     */
    @Override
    public List<Category> treeSelect(Long parentId) {
        //1.根据传入父级分类ID查询分类列表
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, parentId);
        queryWrapper.select(Category::getId, Category::getName, Category::getParentId, Category::getStatus, Category::getCreateTime, Category::getImageUrl, Category::getOrderNum);
        List<Category> categoryList = list(queryWrapper);
        //2.判断分类列表中每个分类是否还有子分类，如果有将hasChildren设置为true
        if (!CollectionUtils.isEmpty(categoryList)) {
            for (Category category : categoryList) {
                long count = this.count(new LambdaQueryWrapper<Category>().eq(Category::getParentId, category.getId()));
                category.setHasChildren(count>0);
            }
        }
        return categoryList;
    }

    /**
     * 查询一级分类列表
     *
     * @return
     */
    @Override
    public List<CategoryVo> getLevelOneCategory() {
        //1.查询一级分类列表
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, 0);
        List<Category> categoryList = list(queryWrapper);
        //2.将集合泛型从PO转为VO
        if (!CollectionUtils.isEmpty(categoryList)) {
            List<CategoryVo> categoryVoList = categoryList
                    .stream()
                    .map(category -> {
                        CategoryVo categoryVo = new CategoryVo();
                        BeanUtils.copyProperties(category, categoryVo);
                        return categoryVo;
                    }).collect(Collectors.toList());
            return categoryVoList;
        }
        return null;
    }

    /**
     * @return
     */
    @Override
    public List<CategoryVo> tree() {
        //1.查询所有分类
        List<Category> categoryList = baseMapper.selectList(null);
        //2.将集合泛型从PO转为VO
        if (!CollectionUtils.isEmpty(categoryList)) {
            List<CategoryVo> categoryVoList = categoryList.stream().map(category -> {
                CategoryVo categoryVo = new CategoryVo();
                BeanUtils.copyProperties(category, categoryVo);
                return categoryVo;
            }).collect(Collectors.toList());
            //3.处理所有分类VO
            return this.buildTree(categoryVoList);
        }
        return null;
    }

    /**
     * 处理分类树形结构
     *
     * @param categoryVoList
     * @return
     */
    @Override
    public List<CategoryVo> buildTree(List<CategoryVo> categoryVoList) {
        //1.创建集合保存所有顶级分类
        List<CategoryVo> topCategoryList = new ArrayList<>();


        //2.为了能够快速得到某个分类对象，将所有分类转为Map Map中key为分类id，value为分类对象
        Map<Long, CategoryVo> categoryVoMap = categoryVoList
                .stream()
                .collect(Collectors.toMap(CategoryVo::getId, t -> t));

        //3.遍历所有分类VO列表 找出所有顶级分类
        for (CategoryVo categoryVo : categoryVoList) {
            if (categoryVo.getParentId() == 0) {
                //将共计10个顶级分类加入到响应结果集合中
                topCategoryList.add(categoryVo);
            } else {
                //说明是子分类 找出当前子分类上级分类
                Long parentId = categoryVo.getParentId();
                //从所有分类Map中获取到上级分类对象
                categoryVoMap.get(parentId).getChildren().add(categoryVo);
            }
        }
        return topCategoryList;
    }
}
